import { _decorator, Component, Enum, EventMouse, Node, Sprite } from 'cc';
import { SunManager } from './manager/SunManager';
import { MouseManager } from './manager/MouseManager';
import { CardState, PlantType } from './Global';
import { AudioManager } from './manager/AudioManager';
import { Config } from './manager/Config';
const { ccclass, property } = _decorator;



@ccclass('Card')
export class Card extends Component {
    private cardState: CardState = CardState.Disable;//卡牌状态
    @property({ type: Enum(PlantType) })
    public plantType: PlantType;

    @property(Node)
    public cardLight: Node = null;//卡牌亮着的节点
    @property(Node)
    public cardGrey: Node = null;//卡牌暗着的节点
    @property(Sprite)
    public cardMask: Sprite = null;//卡牌遮罩
    @property(Number)
    public cdTime: number = 2;//卡牌冷却时间
    private cdTimer: number = 0;//卡牌冷却计时器
    @property({ type: Number, tooltip: "卡牌需要的阳光点数" })
    private needSunPoint: number = 50;//卡牌需要的阳光点数

    start() {
        this.cdTimer = this.cdTime;
    }

    update(deltaTime: number) {
        switch (this.cardState) {
            case CardState.Cooling:
                this.CoolingUpdate(deltaTime);
                break;
            case CardState.WaitingSun:
                this.WaitingSunUpdate();
                break;
            case CardState.Ready:
                this.ReadyUpdate();
                break;
        }
    }

    /**
     * 冷却刷新
     * @param dt 
     */
    private CoolingUpdate(dt: number) {
        this.cdTimer -= dt;
        this.cardMask.fillRange = -(this.cdTimer / this.cdTime);
        if (this.cdTimer <= 0) {
            this.transitionToWaitingSun();
        }
    }
    /**
     * 等待阳光充足刷新
     */
    private WaitingSunUpdate() {
        if (this.needSunPoint <= SunManager.Instance.SunPoint) {
            this.transitionToReady();
        }
    }
    /**
     * 准备种植刷新
     */
    private ReadyUpdate() {
        if (this.needSunPoint > SunManager.Instance.SunPoint) {
            this.transitionToWaitingSun();
        }
    }
    /**
     * 切换到等阳光充足状态
     */
    transitionToWaitingSun() {
        this.cardState = CardState.WaitingSun;
        this.cardLight.active = false;
        this.cardGrey.active = true;
        this.cardMask.node.active = false;
    }
    /**
     * 切换到准备种植状态
     */
    transitionToReady() {
        this.cardState = CardState.Ready;
        this.cardLight.active = true;
        this.cardGrey.active = false;
        this.cardMask.node.active = false;
    }
    /**
    * 切换到冷却状态
    */
    transitionToCooling() {
        this.cardState = CardState.Cooling;
        this.cdTimer = this.cdTime;
        this.cardLight.active = false;
        this.cardGrey.active = true;
        this.cardMask.node.active = true;
    }

    onClick(event:EventMouse) {
        AudioManager.Instance.playClip(Config.btn_click,2);
        if(this.cardState==CardState.Disable) return;
        // 如果需要的阳光值大于当前阳光值，则直接返回
        if (this.needSunPoint > SunManager.Instance.SunPoint) return;
        // 调用 MouseManager 的 addPlant 方法种植植物，并返回是否成功
        let isSuccess = MouseManager.Instance.addPlant(this.plantType,event);
        if (isSuccess) {
            // 如果种植成功，则从 SunManager 中减去需要的阳光值
            SunManager.Instance.subSun(this.needSunPoint);
            // 调用 transitionToCooling 方法进入冷却状态
            this.transitionToCooling()
        }
    }
    disableCard() {
        this.cardState = CardState.Disable;
    }
    enableCard(){
        this.transitionToCooling();
    }
}



